package pab.ui;

import java.awt.*;
import java.awt.event.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.*;

import pab.ui.auxiliary.Base64;
/*
 * Created by JFormDesigner on Tue Jun 21 12:55:54 ICT 2011
 */



/**
 * @author rukowen
 */
public class InitialPassword extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String propPath = "";
	boolean isOk;
	public InitialPassword(Frame owner, String propPath) {
		super(owner);
		initComponents();
		this.propPath = propPath;
		
		isOk = false;
		setModal(true);
	}

	public InitialPassword(Dialog owner) {
		super(owner);
		initComponents();
	}

	private void btnOkMouseClicked(MouseEvent e) {
		// TODO add your code here
		@SuppressWarnings("deprecation")
		String pass = pwdPass.getText();
		@SuppressWarnings("deprecation")
		String confirm  =pwdConfirm.getText();
		
		if(pass.equals(confirm)){
			
			try {
				Properties prop = new Properties();
				FileOutputStream fos = new FileOutputStream(propPath);
				
				prop.setProperty("user", Base64.encode(pass));  
				prop.store(fos, "null");
				isOk = true;
				fos.close();
				this.dispose();
				
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}else{
			JOptionPane.showMessageDialog(this, "Invalid confirm password");
		}
	}

	private void btnCancelMouseClicked(MouseEvent e) {
		// TODO add your code here
		isOk = false;
		this.dispose();
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		label1 = new JLabel();
		label2 = new JLabel();
		pwdPass = new JPasswordField();
		pwdConfirm = new JPasswordField();
		btnOk = new JButton();
		btnCancel = new JButton();

		//======== this ========
		setTitle("Initial Password");
		Container contentPane = getContentPane();
		contentPane.setLayout(null);

		//---- label1 ----
		label1.setText("Password");
		contentPane.add(label1);
		label1.setBounds(35, 20, 75, label1.getPreferredSize().height);

		//---- label2 ----
		label2.setText("Confirm");
		contentPane.add(label2);
		label2.setBounds(35, 55, 65, label2.getPreferredSize().height);
		contentPane.add(pwdPass);
		pwdPass.setBounds(115, 15, 205, pwdPass.getPreferredSize().height);
		contentPane.add(pwdConfirm);
		pwdConfirm.setBounds(115, 50, 205, pwdConfirm.getPreferredSize().height);

		//---- btnOk ----
		btnOk.setText("Ok");
		btnOk.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				btnOkMouseClicked(e);
			}
		});
		contentPane.add(btnOk);
		btnOk.setBounds(115, 85, 65, btnOk.getPreferredSize().height);

		//---- btnCancel ----
		btnCancel.setText("Cancel");
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				btnCancelMouseClicked(e);
			}
		});
		contentPane.add(btnCancel);
		btnCancel.setBounds(195, 85, 120, btnCancel.getPreferredSize().height);

		contentPane.setPreferredSize(new Dimension(400, 170));
		pack();
		setLocationRelativeTo(getOwner());
		// JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JLabel label1;
	private JLabel label2;
	private JPasswordField pwdPass;
	private JPasswordField pwdConfirm;
	private JButton btnOk;
	private JButton btnCancel;
	// JFormDesigner - End of variables declaration  //GEN-END:variables
}
